package com.zzg.cloud.util;

import com.xxl.job.core.log.XxlJobFileAppender;
import com.xxl.job.core.util.DateUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.helpers.FormattingTuple;
import org.slf4j.helpers.MessageFormatter;
import java.io.StringWriter;
import java.util.Date;

/**
 * 用于XXLLogger的通用类，方便同时输出xxlJob和系统日志
 **/
public class XXLLogUtil {
    private XXLLogUtil() {
    }

    private static Logger logger = LoggerFactory.getLogger("xxl-job logger");

    public static void log(String appendLogPattern, Object... appendLogArguments) {
        logInfo(appendLogPattern, appendLogArguments);
    }

    public static void log(Throwable e) {
        logException(e);
    }

    public static void info(String appendLogPattern, Object... appendLogArguments) {
        logInfo(appendLogPattern, appendLogArguments);
    }

    public static void error(String appendLogPattern, Object... appendLogArguments) {
        FormattingTuple ft = MessageFormatter.arrayFormat(appendLogPattern, appendLogArguments);
        String appendLog = ft.getMessage();
        StackTraceElement callInfo = (new Throwable()).getStackTrace()[1];
        Logger log = LoggerFactory.getLogger(callInfo.getClassName());
        log.error(appendLog, ft.getThrowable());
        logDetail(callInfo, appendLog);
    }

    public static void error(Throwable e) {
        logException(e);
    }

    private static void logException(Throwable e) {
        StringWriter stringWriter = new StringWriter();
        stringWriter.append(ExceptionUtils.getStackTrace(ObjectUtils.defaultIfNull(e.getCause(), e)));
        String appendLog = stringWriter.toString();
        StackTraceElement callInfo = (new Throwable()).getStackTrace()[2];
        Logger log = LoggerFactory.getLogger(callInfo.getClassName());
        log.error("xxl job error", e);
        logDetail(callInfo, appendLog);
    }

    private static void logInfo(String appendLogPattern, Object... appendLogArguments) {
        FormattingTuple ft = MessageFormatter.arrayFormat(appendLogPattern, appendLogArguments);
        String appendLog = ft.getMessage();
        StackTraceElement callInfo = (new Throwable()).getStackTrace()[2];
        Logger log = LoggerFactory.getLogger(callInfo.getClassName());
        log.info(appendLogPattern, appendLogArguments);
        logDetail(callInfo, appendLog);
    }


    private static void logDetail(StackTraceElement callInfo, String appendLog) {
        String formatAppendLog = DateUtil.formatDateTime(new Date())
                + " " + "[" + callInfo.getClassName() + "#" + callInfo.getMethodName() + "]" + "-" + "[" + callInfo.getLineNumber() + "]" + "-" + "[" + Thread.currentThread().getName() + "]" + " " + (appendLog != null ? appendLog : "");
        String logFileName = XxlJobFileAppender.contextHolder.get();
        if (logFileName != null && logFileName.trim().length() > 0) {
            XxlJobFileAppender.appendLog(logFileName, formatAppendLog);
        } else {
            logger.info(">>>>>>>>>>> {}", formatAppendLog);
        }

    }
}
